﻿using MediatR;
using Process.Api.Application.Command;
using Process.BLL;
using Process.DAL;
using Process.ErrorCod;
using Process.Model;

namespace Process.Api.Application.Handler
{
    public class DetailProcessinfoHandler : IRequestHandler<DetailProcessinfoCommand, ApiResult<Processinfo>>
    {
        private readonly IProcessRepository<Processinfo> processinfoRepo;

        public DetailProcessinfoHandler(IProcessRepository<Processinfo> processinfoRepo)
        {
            this.processinfoRepo = processinfoRepo;
        }
        /// <summary>
        /// 根据Id查询工序列表
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public Task<ApiResult<Processinfo>> Handle(DetailProcessinfoCommand request, CancellationToken cancellationToken)
        {
            var result = new ApiResult<Processinfo>();

            var entity = processinfoRepo.GetAllAsync().FirstOrDefault(x => x.Id == request.Id && x.IsDelete == false);
            if (entity == null)
            {
                result.Code = ErrorStatus.Error;
                result.Message = "未找到对应工序分类";
                return Task.FromResult(result);
            }

            result.Code = ErrorStatus.Success;
            result.Data = entity;
            result.Message = "获取成功";
            return Task.FromResult(result);
        }
    }
}
